using Config;
using FrameWork.ModSystem;
using HarmonyLib;
using System;
using TaiwuModdingLib.Core.Plugin;
using TMPro;
using UnityEngine;
using Object = UnityEngine.Object;
using Vector3 = UnityEngine.Vector3;

namespace CricketGetGood
{

    [PluginConfig("CricketGetGoodMod", "Zyy", "1.0.0")]
    public class CricketGetGoodMod : TaiwuRemakePlugin
    {

        public static bool EnableFlag;

        public static bool SetSingFlag;

        private Harmony harmony;

        public override void Dispose()
        {
            CricketGetGoodMod.Clean();
            bool flag = this.harmony != null;
            if (flag)
            {
                this.harmony.UnpatchSelf();
            }
        }

        public override void Initialize()
        {
            this.harmony = Harmony.CreateAndPatchAll(typeof(CricketGetGoodMod), null);
        }

        public override void OnModSettingUpdate()
        {
            ModManager.GetSetting(base.ModIdStr, "EnableFlag", ref CricketGetGoodMod.EnableFlag);
            ModManager.GetSetting(base.ModIdStr, "SetSingFlag", ref CricketGetGoodMod.SetSingFlag);
        }

        [HarmonyPostfix]
        [HarmonyPatch(typeof(UI_CatchCricket), "InitCatchPlace")]
        public static void InitCatchPlace_PostPatch()
        {
            bool flag = !CricketGetGoodMod.EnableFlag;
            if (flag)
            {
                CricketGetGoodMod.Clean();
            }
            else
            {
                UI_CatchCricket root = UIElement.CatchCricket.UiBaseAs<UI_CatchCricket>();
                Traverse traverse = Traverse.Create(root);
                UI_CatchCricket.CricketPlaceInfo[] value = traverse.Field("_catchPlaceList").GetValue<UI_CatchCricket.CricketPlaceInfo[]>();
                RectTransform value2 = traverse.Field("_catchPlaceRoot").GetValue<RectTransform>();
                for (int i = 0; i < 21; i++)
                {
                    UI_CatchCricket.CricketPlaceInfo cricketPlaceInfo = value[i];
                    CricketPartsItem cricketPartsItem = CricketParts.Instance[cricketPlaceInfo.CricketColorId];
                    CricketPartsItem cricketPartsItem2 = CricketParts.Instance[cricketPlaceInfo.CricketPartsId];
                    string str = (cricketPlaceInfo.CricketPartsId > 0) ? ((cricketPartsItem.NameOrder >= cricketPartsItem2.NameOrder) ? (cricketPartsItem.Name[0] + cricketPartsItem2.Name[0]) : (cricketPartsItem2.Name[0] + cricketPartsItem.Name[0])) : cricketPartsItem.Name[0];
                    int gradeLevel = (cricketPlaceInfo.CricketPartsId > 0) ? Mathf.Max((int)cricketPartsItem.Level, (int)cricketPartsItem2.Level) : ((int)cricketPartsItem.Level);
                    str += $"singLevel:{cricketPartsItem2.SingSize} SingPitch:{cricketPartsItem2.SingPitch}";
                    string text = str.SetGradeColor(gradeLevel);
                    Transform transform = value2.GetChild(i).transform;
                    Transform transform2 = transform.Find("cricketShowName");
                    if (transform2 == null)
                    {
                        TextMeshProUGUI textMeshProUGUI = GameObjectCreationUtils.UGUICreateTMPText(transform, text, 0, 0, 100, 100, 36f);
                        textMeshProUGUI.transform.gameObject.name = "cricketShowName";
                        textMeshProUGUI.fontSize = 25f;
                        textMeshProUGUI.transform.localPosition = new Vector3(100f, -100f);
                    }
                    else
                    {
                        transform2.GetComponent<TextMeshProUGUI>().text = text;
                    }
                }
            }
        }

        public static void Clean()
        {
            UI_CatchCricket ui_CatchCricket = UIElement.CatchCricket.UiBaseAs<UI_CatchCricket>();
            bool flag = ui_CatchCricket != null;
            if (flag)
            {
                Traverse traverse = Traverse.Create(ui_CatchCricket);
                RectTransform value = traverse.Field("_catchPlaceRoot").GetValue<RectTransform>();
                for (int i = 0; i < 21; i++)
                {
                    Transform transform = value.GetChild(i).transform;
                    Transform transform2 = transform.Find("cricketShowName");
                    if (transform2 != null)
                    {
                        Object.Destroy(transform2.gameObject);
                    }
                }
            }
        }

        [HarmonyPrefix]
        [HarmonyPatch(typeof(UI_CatchCricket), "OnClickCatchPlace", new Type[]
        {
            typeof(int)
        })]
        public static void OnClickCatchPlace_PrePatch(int index)
        {
            bool flag = !CricketGetGoodMod.SetSingFlag;
            if (!flag)
            {
                UI_CatchCricket root = UIElement.CatchCricket.UiBaseAs<UI_CatchCricket>();
                Traverse traverse = Traverse.Create(root);
                UI_CatchCricket.CricketPlaceInfo[] value = traverse.Field("_catchPlaceList").GetValue<UI_CatchCricket.CricketPlaceInfo[]>();
                value[index].SingLevel = 1000;
            }
        }


    }
}
